package com.imooc.bilibili.controller;

import com.imooc.bilibili.result.Result;
import com.imooc.bilibili.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file-upload")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/md5-files")
    public Result<String> getFileMD5(MultipartFile file) throws IOException {
        String fileMD5 = fileService.getFileMD5(file);
        return new Result<>(fileMD5);
    }

    @PutMapping("/file-slices")
    public Result<String> uploadFileBySlices(MultipartFile file,
                                             String fileMD5,
                                             Integer sliceNo,
                                             Integer totalSliceNo) throws Exception {
        String filePath = fileService.uploadFileBySlices(file,fileMD5,sliceNo,totalSliceNo);
        return new Result<>(filePath);
    }
}
